module AboutYou
  module SDK
    module Model
      ###
      # BasketVairantItem is a class representing a basket variant item
      ###
      module BasketVariantItem
        # the json object from which this instance was created from
        attr_accessor :json_object
        # the product of this variant item
        attr_accessor :product
        # the variant of this variant item
        attr_accessor :variant
        # the variant id of this variant item
        attr_accessor :variant_id
        # the app id of this variant item
        attr_accessor :app_id

        ###
        # Constructor for the AboutYou::SDK::Model::BasketVariantItem class
        #
        # * *Args*    :
        #   - +variant_id+ -> the id of the variant item
        #   - +additional_data+ -> additional data of the variant item
        #   - +app_id+ -> the app id for the variant item
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::BasketVariantItem
        ###
        def initialize(variant_id, additional_data = nil, app_id = nil)
          check_variant_id(variant_id)
          check_addition_data(additional_data)
          self.variant_od = variant_id
          self.additional_data = additional_data

          return self unless app_id

          check_app_id(app_id)
          self.app_id = app_id

          self
        end

        ###
        # This method checks if there are errors in the variant item
        #
        # * *Returns* :
        #   - Boolean determining whether there are errors or not
        ###
        def errors?
          error_code > 0
        end

        ###
        # Getter for the total price
        #
        # * *Returns* :
        #   - an Integer containing the total price
        ###
        def total_price
          json_object['total_price']
        end

        ###
        # Getter for the tax
        #
        # * *Returns* :
        #   - an Integer containing the tax
        ###
        def tax
          json_object['tax']
        end

        ###
        # Getter for the total vat
        #
        # * *Returns* :
        #   - an Integer containing the total vat
        ###
        def total_vat
          json_object['total_vat']
        end

        ###
        # Getter for the total net
        #
        # * *Returns* :
        #   - an Integer containing the total net
        ###
        def total_net
          json_object['total_net']
        end

        ###
        # Getter for the old price of the variant
        #
        # * *Returns* :
        #   - an Integer containing the old price of the variant
        ###
        def old_price
          variant.old_price
        end

        ###
        # Getter for the variant
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::Variant
        ###
        def variant
          self.variant = product.variant_by_id(variant_id) unless
          @variant && product

          @variant
        end

        ###
        # This method is used for creating a unique key for the basket set
        #
        # * *Returns* :
        #   - a String containing a unique key for the basket set
        ###
        def unique_key
          key = variant_id
          if additionalData
            additionalData.sort!
            key += ':' + additionalData.to_json
          end

          key
        end

        ###
        # This method checks if a variant id is valid
        #
        # * *Fails* :
        #   - if variant_id is not an integer
        ###
        def check_variant_id(variant_id)
          fail '\InvalidArgumentException! the variant id must be
            an integer' unless variant_id.is_a?(Integer)
        end

        ###
        # This method checks if an app id is valid
        #
        # * *Fails* :
        #   - if app_id is not an integer
        ###
        def check_app_id(app_id)
          fail '\InvalidArgumentException! the app id must be
            an integer' unless app_id.is_a?(Integer)
        end
      end
    end
  end
end
